package com.ruoyi.eqmonitor.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.eqmonitor.service.IPipeService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPOutputStream;

/**
 * 管网服务
 */
@Slf4j
@RestController
@RequestMapping("/pipe")
public class PipeController {

    @Autowired
    private IPipeService iPipeService;

    /**
     * 获取管网JSON数据
     */
    @Deprecated
    @ApiOperation(value = "获取管网JSON数据")
    @RequestMapping("/getPipeJson")
    public void receivePostBody(@RequestParam("code") String code, HttpServletRequest request, HttpServletResponse response) {
        R<List<Map<String, Object>>> r = R.ok(iPipeService.getPipeJsonByCode(code));
        //将实体对象转换为JSON Object转换
        JSONObject jsonObject = JSONUtil.parseObj(r);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Cache-Control", "max-age="+(48*60));
        response.setDateHeader("Expires",System.currentTimeMillis()+48*60*60*1000);
        response.setContentType("application/json; charset=utf-8");
        response.setHeader("Content-Encoding", "gzip");
//        System.out.println("转换前"+ jsonObject.toString().getBytes(StandardCharsets.UTF_8).length);
        try (
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                OutputStream responseOut = response.getOutputStream();
                GZIPOutputStream gzip = new GZIPOutputStream(out)
        ){
            byte[] bytes = jsonObject.toString().getBytes(StandardCharsets.UTF_8);
            gzip.write(bytes);
            gzip.finish();
            byte[] byteArr = out.toByteArray();
            responseOut.write(byteArr);
            response.setContentLength(byteArr.length);
//            System.out.println("转换后"+ out.toByteArray().length);
        } catch (IOException e) {
            log.error("获取管网JSON数据失败", e);
        }
    }

    /**
     * 根据物探点号查询管点数据
     */
    @ApiOperation(value = "根据物探点号查询管点数据")
    @RequestMapping("/getPointByWtdh")
    public AjaxResult getPointByWtdh(@RequestParam("pipeType") String pipeType, @RequestParam("WTDH") String WTDH) {
        return AjaxResult.success(iPipeService.getPointByWtdh(pipeType, WTDH));
    }

    /**
     * 根据QDDH与ZDDH查询管线数据
     */
    @ApiOperation(value = "根据QDDH与ZDDH查询管线数据")
    @RequestMapping("/getLineByQddhAndZddh")
    public AjaxResult getLineByQddhAndZddh(@RequestParam("pipeType") String pipeType, @RequestParam("QDDH") String QDDH, @RequestParam("ZDDH") String ZDDH) {
        return AjaxResult.success(iPipeService.getLineByQddhAndZddh(pipeType, QDDH, ZDDH));
    }

    /**
     * 根据管点物探点号获取管线列表
     */
    @ApiOperation(value = "根据管点物探点号获取管线列表")
    @RequestMapping("/getLineListByWtdh")
    public AjaxResult getLineListByWtdh(@RequestParam("pipeType") String pipeType, @RequestParam("WTDH") String WTDH) {
        return AjaxResult.success(iPipeService.getLineByPointWtdh(pipeType, WTDH));
    }
}
